D:\a\tools.proto\tools.proto\dynamic\src\proto\core.rs
Line | Count | Source |
1 | | // Copyright (c) 2025, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use std::collections::HashMap; |
30 | | use std::rc::Rc; |
31 | | use bp3d_protoc::api::core::loader::Loader; |
32 | | use bp3d_protoc::compiler::Protocol; |
33 | | use bp3d_protoc::compiler::r#enum::Enum; |
34 | | use bp3d_protoc::compiler::structure::Structure; |
35 | | use bp3d_protoc::compiler::union::Union; |
36 | | use bp3d_protoc::compiler::util::imports::ImportSolver; |
37 | | use crate::component::factory::Factory; |
38 | | use crate::proto::message_ext::MessageExt; |
39 | | |
40 | | struct Solver; |
41 | | |
42 | | impl ImportSolver for Solver { |
43 | 284 | fn get_full_type_path(&self, protocol: &Protocol, type_name: &str) -> Option<String> { |
44 | 284 | let package = protocol.package(); |
45 | 284 | if package.is_empty() { Branch (45:12): [Folded - Ignored]
Branch (45:12): [True: 0, False: 284]
|
46 | 0 | Some(format!("{}.{}.{}", package, protocol.name(), type_name)) |
47 | | } else { |
48 | 284 | Some(format!("{}.{}", protocol.name(), type_name)) |
49 | | } |
50 | 284 | } |
51 | | } |
52 | | |
53 | | pub struct Proto { |
54 | | structures: HashMap<String, Rc<Structure>>, |
55 | | messages: HashMap<String, Rc<MessageExt>>, |
56 | | unions: HashMap<String, Rc<Union>>, |
57 | | enums: HashMap<String, Rc<Enum>>, |
58 | | } |
59 | | |
60 | | impl Proto { |
61 | 4 | pub fn build(loader: Loader, factory: Factory) -> Result<Self, bp3d_protoc::api::core::Error> { |
62 | 4 | let mut proto = Proto { |
63 | 4 | structures: Default::default(), |
64 | 4 | messages: Default::default(), |
65 | 4 | unions: Default::default(), |
66 | 4 | enums: Default::default(), |
67 | 4 | }; |
68 | 4 | let factory = Rc::new(factory); |
69 | 4 | let store = loader.compile(&Solver)?0 ; |
70 | 72 | for entry in store4 .entries4 () { |
71 | 72 | if entry.userdata.is_excluded_from_generation() { Branch (71:16): [Folded - Ignored]
Branch (71:16): [True: 0, False: 72]
|
72 | | // Do not build excluded protocols |
73 | 0 | continue; |
74 | 72 | } |
75 | 120 | for value in entry.model.structs72 .iter72 () { |
76 | 120 | proto.structures.insert(store.get_full_type_path(&entry.model, &value.name).unwrap(), value.clone()); |
77 | 120 | } |
78 | 72 | for value12 in entry.model.enums.iter() { |
79 | 12 | proto.enums.insert(store.get_full_type_path(&entry.model, &value.name).unwrap(), value.clone()); |
80 | 12 | } |
81 | 72 | for value12 in entry.model.unions.iter() { |
82 | 12 | proto.unions.insert(store.get_full_type_path(&entry.model, &value.name).unwrap(), value.clone()); |
83 | 12 | } |
84 | 72 | for value64 in entry.model.messages.iter() { |
85 | 64 | proto.messages.insert(store.get_full_type_path(&entry.model, &value.name).unwrap(), Rc::new(MessageExt { |
86 | 64 | factory: factory.clone(), |
87 | 64 | message: value.clone() |
88 | 64 | })); |
89 | 64 | } |
90 | | } |
91 | 4 | Ok(proto) |
92 | 4 | } |
93 | | |
94 | 3 | pub fn get_structure(&self, name: &str) -> Option<&Rc<Structure>> { |
95 | 3 | self.structures.get(name) |
96 | 3 | } |
97 | | |
98 | 2 | pub fn get_message(&self, name: &str) -> Option<&Rc<MessageExt>> { |
99 | 2 | self.messages.get(name) |
100 | 2 | } |
101 | | } |